// 1. 初始化reducer，初始化状态等
const initialState = {
  allList: [] as ChannelType[],
  userList: [] as ChannelType[],
};

export default function channelReducer(
  state = initialState,
  action: ChannelActionType
): typeof initialState {
  switch (action.type) {
    // 4. 编写保存的逻辑
    case 'channel/saveChannels':
      return {
        ...state,
        allList: action.payload,
      };
    case 'channel/saveUserChannels':
      return {
        ...state,
        userList: action.payload,
      };
    default:
      return state;
  }
}

type ChannelType = {
  id: number;
  name: string;
};
type ChannelActionType =
  | {
      type: 'channel/saveChannels';
      payload: ChannelType[];
    }
  | {
      type: 'channel/saveUserChannels';
      payload: ChannelType[];
    };
